package com.verisette.ncos.web.accesscontrol.action;

import java.util.List;

import org.corp.agi.common.annotation.RequiresAuthentication;
import org.corp.agi.service.bean.ServiceResponse;
import org.corp.agi.web.action.BaseAction;
import org.corp.agi.web.util.ServiceRequestFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.opensymphony.xwork2.ActionSupport;
import com.verisette.ncos.persistence.domain.MasterUserGroup;
import com.verisette.ncos.service.master.gateway.UserGroupAuthorizationService;

@Component
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class MainAction extends BaseAction {
	private static final long serialVersionUID = 965718646620439848L;
	
	UserGroupAuthorizationService userGroupAuthorizationService;
	@Autowired
	@Required
	public void setUserGroupAuthorizationService(
			UserGroupAuthorizationService userGroupAuthorizationService) {
		this.userGroupAuthorizationService = userGroupAuthorizationService;
	}

	@RequiresAuthentication(granted = {})
	public String init() throws Exception {
		ServiceResponse<List<MasterUserGroup>> serviceResponse = userGroupAuthorizationService.inquiryUserGroupByUserProfile(ServiceRequestFactory.getNewServiceRequest(httpServletRequest));
		if(isServiceResponseSuccess(serviceResponse)){
			List<MasterUserGroup> userGroupList = serviceResponse.getValueObject();
			if(userGroupList!=null){
				for(MasterUserGroup userGroup : userGroupList) {
					String usgName = userGroup.getUsgName();
					if(usgName.indexOf("Approver")>-1 || usgName.indexOf("approver")>-1){
						if(usgName.equals("Approver2")){
							return "approve2";
						}
						return "approve";
					} else if(usgName.indexOf("Printer")>-1 || usgName.indexOf("printer")>-1 ) {
						return "print";
					}
				}
			}
		}
		return ActionSupport.SUCCESS;
	}
}
